package com.example.hhhh;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.hhhh.permission.PermissionListener;
import com.example.hhhh.permission.PermissionsUtil;
import com.example.hhhh.pick.PickImageActivity;
import com.example.hhhh.pick.PickImageActivity.PickImageAction;
import com.example.hhhh.util.EnvUtil;

import java.io.File;

/**
 * @ProjectName: Hhhh
 * @Package: com.example.hhhh
 * @ClassName: EighthAct
 * @Description: java类作用描述
 * @Author: songhua
 * @CreateDate: 2020/4/8 0008 14:11
 */
@Route(path = "/app/ElevenAct")
public class ElevenAct extends SwipeBackActivity implements View.OnClickListener {

    private String mPicPath;


    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (!TextUtils.isEmpty(mPicPath)) {
                // 设置头像
                setHeaderImage(mPicPath);
                // 保存用户信息
//                saveData();
            }
        }

        ;
    };
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_eleven);
        btn= (Button) findViewById(R.id.btn);
        btn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        showTypeDialog();
    }

    private void setHeaderImage(String path) {
        // 设置头像
//        if (null != mHeadImg) {
//            ImageLoaderUtil.loadCircleImg(this, mHeadImg, path, R.drawable.headportrait_default_96x96);
//            mHeadImg.invalidate();

//        }
    }

    private void showTypeDialog() {
        //显示对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_select_photo, null);
        TextView tv_select_gallery = (TextView) view.findViewById(R.id.tv_select_gallery);
        TextView tv_select_camera = (TextView) view.findViewById(R.id.tv_select_camera);
        tv_select_gallery.setOnClickListener(new View.OnClickListener() {// 在相册中选取
            @Override
            public void onClick(View v) {
                verifyStoragePermissions();
                dialog.dismiss();
            }
        });
        tv_select_camera.setOnClickListener(new View.OnClickListener() {// 调用照相机
            @Override
            public void onClick(View v) {
                verifyCameraPermissions();
                dialog.dismiss();
            }
        });
        dialog.setView(view);
        dialog.show();
    }

    /**
     * 动态申请图库权限
     */
    public void verifyStoragePermissions() {
        String[] PERMISSIONS_STORAGE = {
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (PermissionsUtil.hasPermission(this, PERMISSIONS_STORAGE)) {
//            showPicture();
            getImgFromAlbum();
        } else {
            PermissionsUtil.requestPermission(this, new PermissionListener() {
                @Override
                public void permissionGranted(@NonNull String[] permission) {

//                    showPicture();
                    getImgFromAlbum();
                }

                @Override
                public void permissionDenied(@NonNull String[] permission) {
                    show( "用户拒绝使用此权限！");

                }
            }, "相册", PERMISSIONS_STORAGE);
        }
    }

    /**
     * 动态申请相机权限
     */
    public void verifyCameraPermissions() {
        String[] PERMISSIONS_CAMERA_STORAGE = {
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA};
        if (PermissionsUtil.hasPermission(this, PERMISSIONS_CAMERA_STORAGE)) {
//            takePhoto();
            getImgFromCamera();
        } else {
            PermissionsUtil.requestPermission(this, new PermissionListener() {
                @Override
                public void permissionGranted(@NonNull String[] permission) {
//                    takePhoto();
                    getImgFromCamera();
                }

                @Override
                public void permissionDenied(@NonNull String[] permission) {
                    show( "用户拒绝使用此权限！");
                }
            }, "相机", PERMISSIONS_CAMERA_STORAGE);
        }
    }






    // 从相册选择图片
    private void getImgFromAlbum() {
        Intent intent = new Intent(this,PickImageActivity.class);
        intent.putExtra(PickImageAction.EXTRA_MODE, PickImageAction.MODE_ALBUM);
        startActivityForResult(intent, PickImageActivity.PickImageAction.MODE_ALBUM);
    }

    // 从相机拍照获取图片
    private void getImgFromCamera() {
        Intent intent = new Intent(this,PickImageActivity.class);
        intent.putExtra(PickImageAction.EXTRA_MODE, PickImageAction.MODE_CAMERA);
        startActivityForResult(intent, PickImageAction.MODE_CAMERA);
    }

    //裁剪图片
    private void startPhotoCut(String path) {
//        Intent intent = new Intent(FusionIntent.SETTING_IMG);
        Intent intent = new Intent(this,PickImageActivity.class);
        intent.putExtra(PickImageAction.EXTRA_MODE, PickImageAction.CROP_IMG);
        intent.putExtra(PickImageAction.EXTRA_IMAGE_PATH, path);
        intent.putExtra(PickImageAction.EXTRA_OUTPUT_X, 300);
        intent.putExtra(PickImageAction.EXTRA_OUTPUT_Y, 300);
        startActivityForResult(intent, PickImageAction.CROP_IMG);
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == PickImageAction.MODE_ALBUM
                || requestCode == PickImageAction.MODE_CAMERA) {
            if (!EnvUtil.isExitSDCard()) {
                show("检测不到SDCard");
            } else {
                if (intent != null) {
                    mPicPath = intent
                            .getStringExtra(PickImageAction.EXTRA_IMAGE_PATH);
                    if (!TextUtils.isEmpty(mPicPath)) {
//                        show("路径："+mPicPath);
                        Log.e("Elevent","路径："+mPicPath);
                        File imageFile = new File(mPicPath);
                        if (imageFile.exists()) {
                            startPhotoCut(mPicPath);
                        }
                        imageFile = null;
                    }
                }
            }
        } else if (requestCode == PickImageAction.CROP_IMG) {
            if (intent != null) {
                mPicPath = intent.getStringExtra(PickImageAction.EXTRA_IMAGE_PATH);
                handler.sendEmptyMessageDelayed(11, 500);
            }
        }
    }


    /**
     * 显示toast
     *
     * @param desc
     */
    protected void show(String desc) {
        if (!TextUtils.isEmpty(desc)) {
            Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
        }
    }


}
